import datetime
from django.db import models
from mdeditor.fields import MDTextField

from users.models import Author


class Tag(models.Model):
    """标签"""

    tag_name = models.CharField(max_length=24, verbose_name='标签名称')

    def __str__(self):
        return self.tag_name

    class Meta:
        db_table = 'tb_tag'
        verbose_name = '标签'
        verbose_name_plural = verbose_name


class Category(models.Model):
    """文章分类"""

    cate_name = models.CharField(max_length=24, verbose_name='文章分类')

    def __str__(self):
        return self.cate_name

    class Meta:
        db_table = 'tb_category'
        verbose_name = '文章分类'
        verbose_name_plural = verbose_name


class Article(models.Model):
    """文章"""

    title = models.CharField(max_length=30, verbose_name='文章标题')
    desc = models.CharField(max_length=30, verbose_name='文章简介')
    # content = models.TextField(verbose_name='文章内容')
    content = MDTextField(verbose_name='文章内容')
    # help_text = models.CharField(max_length=300, verbose_name='文章title')
    img_url = models.ImageField(upload_to='article/%Y%m%d/', blank=True)
    author = models.ForeignKey(to=Author, on_delete=models.CASCADE, verbose_name='作者')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    view_count = models.IntegerField(verbose_name='观看')
    up = models.IntegerField(verbose_name='顶')
    down = models.IntegerField(verbose_name='踩')
    category = models.ForeignKey(to=Category, related_name='articles', on_delete=models.SET_NULL, null=True)
    tags = models.ManyToManyField(to=Tag, related_name='articles', blank=True)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ('-create_time',)
        db_table = 'tb_article'
        verbose_name = '文章管理'
        verbose_name_plural = verbose_name


class Comment(models.Model):
    """评论"""

    content = models.TextField()
    article = models.ForeignKey(to=Article, on_delete=models.SET_NULL, null=True)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    def __str__(self):
        return self.article.title

    class Meta:
        db_table = 'tb_comment'
        verbose_name = '评论管理'
        verbose_name_plural = verbose_name
